package com.dragee.controller;

import com.dragee.entity.TextMsg;
import com.dragee.service.IMsg;
import com.dragee.service.impl.TextMsgImpl;
import com.dragee.util.MsgUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author tys on 9/17/20
 */
@RestController
public class MsgController {

    @RequestMapping(value = "wx",method= RequestMethod.POST)
    public void acceptMsg(HttpServletRequest request, HttpServletResponse response) throws InstantiationException, IllegalAccessException {
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        String message = null;
        IMsg msg = new TextMsgImpl();

        TextMsg input = MsgUtil.xmlToBean(request, TextMsg.class);
        System.out.println("传入方式------> " + input.getMsgType());

        message = msg.initMessage(input.getFromUserName(), input.getToUserName(), "你录入的是" + input.getContent(), "text");

        try {
            out = response.getWriter();
            out.write(message);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        out.close();
    }
}
